﻿<?php
if(!defined("YOURPHP")) exit("Access Denied");
class IndexAction extends BaseAction
{
    public function index()
    {
    	
   		if(isset($_SESSION['editsave'])){unset($_SESSION['editsave']);}
   		if(!$_POST['item_number']&&$_SESSION['domain']){
   			
   			$_POST['domain']=$_SESSION['domain'];
   			$_POST['currency_code']=$_SESSION['currency_code'];
   			//if(isset($_SESSION['domain'])){unset($_SESSION['domain']);}
   			//if(isset($_SESSION['currency_code'])){unset($_SESSION['currency_code']);}
   			$this->cart1();
   		}elseif ($_POST['item_number']){
   			
			$domain=trim($_POST['domain']);//域名
			$currency_code=trim($_POST['currency_code']);//币种
			$item_number=trim($_POST['item_number']);//产品编码
			$item=trim($_POST['item']);//产品
			$price=trim($_POST['price']);//价格
			$hlt=trim($_POST['hlt']);//数量
			//$car_number=rand();
	
			$_SESSION['a']['domain']=$domain;
			$_SESSION['a']['currency_code']=$currency_code;
			//$_SESSION['a']['item_number']=$item_number;
			
			
	
			$_SESSION['product'][$domain][$item_number]=array('item'=>$item,'price'=>$price,'num'=>1);
		/**
					if(!empty($_SESSION['cart1'])){
						
					
					}else{
						$_SESSION['cart1']=$car_number;
						
					}
		*/
   					 if(!empty($_SESSION['cart1'])){
						if(false === strripos($_SESSION['cart1'],$item_number)){
							$_SESSION['cart1'].=','.$item_number;
						}
					}else{
						$_SESSION['cart1']=$item_number;
					}
		
				//$this->redirect('cart1');
				
   			}
   			
   			$this->cart1();
	}
	
	public function paypal_print(){
		echo "paypal print";
	}
    
    
    
    public function cart1()
    {	
    	
    	$map['domain']=$_POST['domain'];
		$map['currency']=$_POST['currency_code'];
    	$siteinfo=M('website')->where($map)->find();
    	
    	
    		$_SESSION['a']['country'] = $siteinfo['country'];
			$_SESSION['a']['lang'] = $siteinfo['langzip'];
			//$_SESSION['a']['saleser'] = $siteinfo['saleser'];
			$_SESSION['a']['rate'] = $siteinfo['abbreviation'];
			$_SESSION['a']['isalert'] = $siteinfo['alert'];
			$_SESSION['a']['email'] = $siteinfo['email'];
    	
    	
    	

    	if(!empty($_SESSION['cart1'])){
			//$prolist=$_SESSION['cart1'];
			$temp_domain=$map['domain'];
			$temp_prolist=$_SESSION['product'][$temp_domain];
	    	foreach ($temp_prolist as $key=>$value){
						$prolist[]=$key;
	    	}
	    	
		    if(count($prolist)<=1){
				$cc=$this->prosplit($prolist[0],$temp_domain);
				
				$_SESSION['a_price']=$cc['n_price'];
				if($cc['num']>1){$proallnum=$cc['num'];}else{$proallnum=1;}
				$a=array($cc);
				
			}else{
				//$split_dir1 = split ('[,.-]', $prolist);
				$split_dir1 = $prolist;
				$a=array();
				$_SESSION['a_price']='';
				for($i=0;$i<count($split_dir1);$i++){
					
					$cc=$this->prosplit($split_dir1[$i],$temp_domain);
					$_SESSION['a_price']+=$cc['n_price'];
					$proallnum+=$cc['num'];
					$a[$i]=$cc;
					
				}
				
			}
	    	
			if($proallnum>1){
				$_SESSION['a']['logisticsprice1'] = $siteinfo['postprice2'];
				$_SESSION['a']['logisticsprice2'] = $siteinfo['postprice4'];
			}else{
				$_SESSION['a']['logisticsprice1'] = $siteinfo['postprice1'];
				$_SESSION['a']['logisticsprice2'] = $siteinfo['postprice3'];
			}
			$_SESSION['a']['proallnum']=$proallnum;
			
			#paypal直接付款
			
			
			
   			//$paypal_print=paypal_print();
   			//$this->assign('paypal_print', $paypal_print);
			
			
			
			$this->assign ( 'a', $a );
			$currency_code=$_POST['currency_code'];
			$this->assign('domain', $map['domain']);
			$this->assign('currency_code', $currency_code);
			$this->assign ( 'currency', $siteinfo['currency'] );
			$this->assign ( 'a_price', $_SESSION['a_price'] );
			$this->assign ( 'a_prices', $_SESSION['a_price']+$_SESSION['a']['logisticsprice1']*$proallnum );
			$this->assign ( 'proallnum', $proallnum );
			
			
			
		}
    	
    		
    		//$_SESSION['a']['template'] = $siteinfo['template'];
    		$template = split ('[,.-]', $siteinfo['template']);
    	
    		$this->display($template[0]);
    	
    	
    	

    }
	public function prosplit($pid,$domain)
	{
			//echo $_POST['n_'.$pid];
			if(!empty($_POST['n_'.$pid])){
				$tmpnum=intval($_POST['n_'.$pid]);
				$_SESSION['product'][$domain][$pid]['num']=intval($_POST['n_'.$pid]);
			}
			if(empty($tmpnum) && !empty($_SESSION['product'][$domain][$pid]['num'])){
				$tmpnum=intval($_SESSION['product'][$domain][$pid]['num']);
			}
			if(empty($tmpnum)){
				$tmpnum=1;
			}
			$aa=array();
			$aa['pbody']=$_SESSION['product'][$domain][$pid]['item'];
			$aa['productcode']=$pid;
			$aa['price']=$_SESSION['product'][$domain][$pid]['price'];
			$aa['num']=$tmpnum;
			$aa['n_price']=$tmpnum*$_SESSION['product'][$domain][$pid]['price'];
			return $aa;
	}
    public function delid()
    {
		if(!empty($_GET['pidmo'])){
			$pid=$_GET['pidmo'];
			$_SESSION['domain']=$_GET['domain'];
			$_SESSION['currency_code']=$_GET['currency_code'];
			$temp_domain=$_SESSION['domain'];
			$prolist=$_SESSION['cart1'];
			$pid=trim($pid);
			print_r($temp_domain);
			if(false!==strripos($prolist,$pid)){
					if($prolist==$pid){
						unset($_SESSION['cart1']);
						unset($_SESSION['a_price']);
						if(isset($_SESSION['product'][$temp_domain][$pid])){unset($_SESSION['product'][$temp_domain][$pid]);}
						
					}else{
						$split_dir1 = split ('[,.-]', $prolist);
						unset($_SESSION['cart1']);
						unset($_SESSION['a_price']);
						if(isset($_SESSION['product'][$temp_domain][$pid])){unset($_SESSION['product'][$temp_domain][$pid]);}
						foreach($split_dir1 as $key1=>$val){
							if($pid!=$val){
								if(empty($_SESSION['cart1'])){$_SESSION['cart1']=$val;}
								else{$_SESSION['cart1'].=','.$val;}
							}
						}
						
					}
			}

		}
		$this->redirect('index');
    }
    
    
    
    public function edit(){
	
    	$order_id=$_POST['orderid'];
    	$order_id_submit=$_POST['orderidsubmit'];
    	$models = M("order");
    	
    	if($order_id){
    	
    	$user_info=$models->where('sn='.$order_id)->find();
    	
    	$update_country=$user_info['country1'];
    	
    	
    	switch ($update_country){
    		
    		case DE:
				$DE='selected=""';
				break;
			case AT:
				$AT='selected=""';
				break;
			case CH:
				$CH='selected=""';
				break;
			case BE:
				$BE='selected=""';
				break;
			case AL:
				$AL='selected=""';
				break;
			case DZ:
				$DZ='selected=""';
				break;
			case AD:
				$AD='selected=""';
				break;
			case AO:
				$AO='selected=""';
				break;
			case AI:
				$AI='selected=""';
				break;
			case AG:
				$AG='selected=""';
				break;
			case AR:
				$AR='selected=""';
				break;
			case AM:
				$AM='selected=""';
				break;
			case AW:
				$AW='selected=""';
				break;
			case AZ:
				$AZ='selected=""';
				break;
			case ET:
				$ET='selected=""';
				break;
			case AU:
				$AU='selected=""';
				break;
			case BS:
				$BS='selected=""';
				break;
			case BH:
				$BH='selected=""';
				break;
			case BB:
				$BB='selected=""';
				break;
			case BZ:
				$BZ='selected=""';
				break;
			case BJ:
				$BJ='selected=""';
				break;
			case BM:
				$BM='selected=""';
				break;
			case BT:
				$BT='selected=""';
				break;
			case BO:
				$BO='selected=""';
				break;
			case BA:
				$BA='selected=""';
				break;
			case BW:
				$BW='selected=""';
				break;
			case BR:
				$BR='selected=""';
				break;
			case VG:
				$VG='selected=""';
				break;
			case BN:
				$BN='selected=""';
				break;
			case BG:
				$BG='selected=""';
				break;
			case BF:
				$BF='selected=""';
				break;
			case BI:
				$BI='selected=""';
				break;
			case KY:
				$KY='selected=""';
				break;
			case CL:
				$CL='selected=""';
				break;
			case C2:
				$C2='selected=""';
				break;
			case CK:
				$CK='selected=""';
				break;
			case CR:
				$CR='selected=""';
				break;
			case DK:
				$DK='selected=""';
				break;
			case DM:
				$DM='selected=""';
				break;
			case 'DO':
				$DO='selected=""';
				break;
			case CD:
				$CD='selected=""';
				break;
			case DJ:
				$DJ='selected=""';
				break;
			case EC:
				$EC='selected=""';
				break;
			case SV:
				$SV='selected=""';
				break;
			case ER:
				$ER='selected=""';
				break;
			case EE:
				$EE='selected=""';
				break;
			case FK:
				$FK='selected=""';
				break;
			case FO:
				$FO='selected=""';
				break;
			case FJ:
				$FJ='selected=""';
				break;
			case FI:
				$FI='selected=""';
				break;
			case FR:
				$FR='selected=""';
				break;
			case GF:
				$GF='selected=""';
				break;
			case PF:
				$PF='selected=""';
				break;
			case GA:
				$GA='selected=""';
				break;
			case GM:
				$GM='selected=""';
				break;
			case GI:
				$GI='selected=""';
				break;
			case GD:
				$GD='selected=""';
				break;
			case GR:
				$GR='selected=""';
				break;
			case GL:
				$GL='selected=""';
				break;
			case GB:
				$GB='selected=""';
				break;
			case GP:
				$GP='selected=""';
				break;
			case GT:
				$GT='selected=""';
				break;
			case GN:
				$GN='selected=""';
				break;
			case GW:
				$GW='selected=""';
				break;
			case GY:
				$GY='selected=""';
				break;
			case HN:
				$HN='selected=""';
				break;
			case HK:
				$HK='selected=""';
				break;
			case IN:
				$IN='selected=""';
				break;
			case ID:
				$ID='selected=""';
				break;
			case IE:
				$IE='selected=""';
				break;
			case IS:
				$IS='selected=""';
				break;
			case IL:
				$IL='selected=""';
				break;
			case IT:
				$IT='selected=""';
				break;
			case JM:
				$JM='selected=""';
				break;
			case JP:
				$JP='selected=""';
				break;
			case YE:
				$YE='selected=""';
				break;
			case JO:
				$JO='selected=""';
				break;
			case KH:
				$KH='selected=""';
				break;
			case CA:
				$CA='selected=""';
				break;
			case CV:
				$CV='selected=""';
				break;
			case KZ:
				$KZ='selected=""';
				break;
			case QA:
				$QA='selected=""';
				break;
			case KE:
				$KE='selected=""';
				break;
			case KG:
				$KG='selected=""';
				break;
			case KI:
				$KI='selected=""';
				break;
			case CO:
				$CO='selected=""';
				break;
			case KM:
				$KM='selected=""';
				break;
			case HR:
				$HR='selected=""';
				break;
			case KW:
				$KW='selected=""';
				break;
			case LA:
				$LA='selected=""';
				break;
			case LS:
				$LS='selected=""';
				break;
			case LV:
				$LV='selected=""';
				break;
			case LI:
				$LI='selected=""';
				break;
			case LT:
				$LT='selected=""';
				break;
			case LU:
				$LU='selected=""';
				break;
			case MG:
				$MG='selected=""';
				break;
			case MW:
				$MW='selected=""';
				break;
			case MY:
				$MY='selected=""';
				break;
			case MV:
				$MV='selected=""';
				break;
			case ML:
				$ML='selected=""';
				break;
			case MT:
				$MT='selected=""';
				break;
			case MA:
				$MA='selected=""';
				break;
			case MH:
				$MH='selected=""';
				break;
			case MQ:
				$MQ='selected=""';
				break;
			case MR:
				$MR='selected=""';
				break;
			case MU:
				$MU='selected=""';
				break;
			case YT:
				$YT='selected=""';
				break;
			case MX:
				$MX='selected=""';
				break;
			case FM:
				$FM='selected=""';
				break;
			case MN:
				$MN='selected=""';
				break;
			case MS:
				$MS='selected=""';
				break;
			case MZ:
				$MZ='selected=""';
				break;
			case NA:
				$NA='selected=""';
				break;
			case NR:
				$NR='selected=""';
				break;
			case NP:
				$NP='selected=""';
				break;
			case NC:
				$NC='selected=""';
				break;
			case NZ:
				$NZ='selected=""';
				break;
			case NI:
				$NI='selected=""';
				break;
			case NL:
				$NL='selected=""';
				break;
			case AN:
				$AN='selected=""';
				break;
			case NE:
				$NE='selected=""';
				break;
			case NU:
				$NU='selected=""';
				break;
			case NF:
				$NF='selected=""';
				break;
			case NO:
				$NO='selected=""';
				break;
			case OM:
				$OM='selected=""';
				break;
			case PW:
				$PW='selected=""';
				break;
			case PA:
				$PA='selected=""';
				break;
			case PG:
				$PG='selected=""';
				break;
			case PE:
				$PE='selected=""';
				break;
			case PH:
				$PH='selected=""';
				break;
			case PN:
				$PN='selected=""';
				break;
			case PL:
				$PL='selected=""';
				break;
			case PT:
				$PT='selected=""';
				break;
			case CG:
				$CG='selected=""';
				break;
			case RE:
				$RE='selected=""';
				break;
			case RW:
				$RW='selected=""';
				break;
			case RO:
				$RO='selected=""';
				break;
			case RU:
				$RU='selected=""';
				break;
			case PM:
				$PM='selected=""';
				break;
			case SB:
				$SB='selected=""';
				break;
			case ZM:
				$ZM='selected=""';
				break;
			case WS:
				$WS='selected=""';
				break;
			case SM:
				$SM='selected=""';
				break;
			case ST:
				$ST='selected=""';
				break;
			case SA:
				$SA='selected=""';
				break;
			case SE:
				$SE='selected=""';
				break;
			case SN:
				$SN='selected=""';
				break;
			case SC:
				$SC='selected=""';
				break;
			case SL:
				$SL='selected=""';
				break;
			case SG:
				$SG='selected=""';
				break;
			case SK:
				$SK='selected=""';
				break;
			case SI:
				$SI='selected=""';
				break;
			case SO:
				$SO='selected=""';
				break;
			case ES:
				$ES='selected=""';
				break;
			case LK:
				$LK='selected=""';
				break;
			case SH:
				$SH='selected=""';
				break;
			case KN:
				$KN='selected=""';
				break;
			case LC:
				$LC='selected=""';
				break;
			case VC:
				$VC='selected=""';
				break;
			case ZA:
				$ZA='selected=""';
				break;
			case KR:
				$KR='selected=""';
				break;
			case SR:
				$SR='selected=""';
				break;
			case SJ:
				$SJ='selected=""';
				break;
			case SZ:
				$SZ='selected=""';
				break;
			case TJ:
				$TJ='selected=""';
				break;
			case TW:
				$TW='selected=""';
				break;
			case TZ:
				$TZ='selected=""';
				break;
			case TH:
				$TH='selected=""';
				break;
			case TG:
				$TG='selected=""';
				break;
			case TO:
				$TO='selected=""';
				break;
			case TT:
				$TT='selected=""';
				break;
			case TD:
				$TD='selected=""';
				break;
			case CZ:
				$CZ='selected=""';
				break;
			case TN:
				$TN='selected=""';
				break;
			case TR:
				$TR='selected=""';
				break;
			case TM:
				$TM='selected=""';
				break;
			case TC:
				$TC='selected=""';
				break;
			case TV:
				$TV='selected=""';
				break;
			case UG:
				$UG='selected=""';
				break;
			case UA:
				$UA='selected=""';
				break;
			case HU:
				$HU='selected=""';
				break;
			case UY:
				$UY='selected=""';
				break;
			case US:
				$US='selected=""';
				break;
			case VU:
				$VU='selected=""';
				break;
			case VA:
				$VA='selected=""';
				break;
			case VE:
				$VE='selected=""';
				break;
			case AE:
				$AE='selected=""';
				break;
			case VN:
				$VN='selected=""';
				break;
			case WF:
				$WF='selected=""';
				break;
			case CY:
				$CY='selected=""';
				break;
			default:
				$Land='selected=""';
    		
    	}
    	
    	
			$this->assign('DE',$DE);
			$this->assign('AT',$AT);
			$this->assign('CH',$CH);
			$this->assign('BE',$BE);
			$this->assign('AL',$AL);
			$this->assign('DZ',$DZ);
			$this->assign('AD',$AD);
			$this->assign('AO',$AO);
			$this->assign('AI',$AI);
			$this->assign('AG',$AG);
			$this->assign('AR',$AR);
			$this->assign('AM',$AM);
			$this->assign('AW',$AW);
			$this->assign('AZ',$AZ);
			$this->assign('ET',$ET);
			$this->assign('AU',$AU);
			$this->assign('BS',$BS);
			$this->assign('BH',$BH);
			$this->assign('BB',$BB);
			$this->assign('BZ',$BZ);
			$this->assign('BJ',$BJ);
			$this->assign('BM',$BM);
			$this->assign('BT',$BT);
			$this->assign('BO',$BO);
			$this->assign('BA',$BA);
			$this->assign('BW',$BW);
			$this->assign('BR',$BR);
			$this->assign('VG',$VG);
			$this->assign('BN',$BN);
			$this->assign('BG',$BG);
			$this->assign('BF',$BF);
			$this->assign('BI',$BI);
			$this->assign('KY',$KY);
			$this->assign('CL',$CL);
			$this->assign('C2',$C2);
			$this->assign('CK',$CK);
			$this->assign('CR',$CR);
			$this->assign('DK',$DK);
			$this->assign('DM',$DM);
			$this->assign('DO',$DO);
			$this->assign('CD',$CD);
			$this->assign('DJ',$DJ);
			$this->assign('EC',$EC);
			$this->assign('SV',$SV);
			$this->assign('ER',$ER);
			$this->assign('EE',$EE);
			$this->assign('FK',$FK);
			$this->assign('FO',$FO);
			$this->assign('FJ',$FJ);
			$this->assign('FI',$FI);
			$this->assign('FR',$FR);
			$this->assign('GF',$GF);
			$this->assign('PF',$PF);
			$this->assign('GA',$GA);
			$this->assign('GM',$GM);
			$this->assign('GI',$GI);
			$this->assign('GD',$GD);
			$this->assign('GR',$GR);
			$this->assign('GL',$GL);
			$this->assign('GB',$GB);
			$this->assign('GP',$GP);
			$this->assign('GT',$GT);
			$this->assign('GN',$GN);
			$this->assign('GW',$GW);
			$this->assign('GY',$GY);
			$this->assign('HN',$HN);
			$this->assign('HK',$HK);
			$this->assign('IN',$IN);
			$this->assign('ID',$ID);
			$this->assign('IE',$IE);
			$this->assign('IS',$IS);
			$this->assign('IL',$IL);
			$this->assign('IT',$IT);
			$this->assign('JM',$JM);
			$this->assign('JP',$JP);
			$this->assign('YE',$YE);
			$this->assign('JO',$JO);
			$this->assign('KH',$KH);
			$this->assign('CA',$CA);
			$this->assign('CV',$CV);
			$this->assign('KZ',$KZ);
			$this->assign('QA',$QA);
			$this->assign('KE',$KE);
			$this->assign('KG',$KG);
			$this->assign('KI',$KI);
			$this->assign('CO',$CO);
			$this->assign('KM',$KM);
			$this->assign('HR',$HR);
			$this->assign('KW',$KW);
			$this->assign('LA',$LA);
			$this->assign('LS',$LS);
			$this->assign('LV',$LV);
			$this->assign('LI',$LI);
			$this->assign('LT',$LT);
			$this->assign('LU',$LU);
			$this->assign('MG',$MG);
			$this->assign('MW',$MW);
			$this->assign('MY',$MY);
			$this->assign('MV',$MV);
			$this->assign('ML',$ML);
			$this->assign('MT',$MT);
			$this->assign('MA',$MA);
			$this->assign('MH',$MH);
			$this->assign('MQ',$MQ);
			$this->assign('MR',$MR);
			$this->assign('MU',$MU);
			$this->assign('YT',$YT);
			$this->assign('MX',$MX);
			$this->assign('FM',$FM);
			$this->assign('MN',$MN);
			$this->assign('MS',$MS);
			$this->assign('MZ',$MZ);
			$this->assign('NA',$NA);
			$this->assign('NR',$NR);
			$this->assign('NP',$NP);
			$this->assign('NC',$NC);
			$this->assign('NZ',$NZ);
			$this->assign('NI',$NI);
			$this->assign('NL',$NL);
			$this->assign('AN',$AN);
			$this->assign('NE',$NE);
			$this->assign('NU',$NU);
			$this->assign('NF',$NF);
			$this->assign('NO',$NO);
			$this->assign('OM',$OM);
			$this->assign('PW',$PW);
			$this->assign('PA',$PA);
			$this->assign('PG',$PG);
			$this->assign('PE',$PE);
			$this->assign('PH',$PH);
			$this->assign('PN',$PN);
			$this->assign('PL',$PL);
			$this->assign('PT',$PT);
			$this->assign('CG',$CG);
			$this->assign('RE',$RE);
			$this->assign('RW',$RW);
			$this->assign('RO',$RO);
			$this->assign('RU',$RU);
			$this->assign('PM',$PM);
			$this->assign('SB',$SB);
			$this->assign('ZM',$ZM);
			$this->assign('WS',$WS);
			$this->assign('SM',$SM);
			$this->assign('ST',$ST);
			$this->assign('SA',$SA);
			$this->assign('SE',$SE);
			$this->assign('SN',$SN);
			$this->assign('SC',$SC);
			$this->assign('SL',$SL);
			$this->assign('SG',$SL);
			$this->assign('SK',$SK);
			$this->assign('SI',$SI);
			$this->assign('SO',$SO);
			$this->assign('ES',$ES);
			$this->assign('LK',$LK);
			$this->assign('SH',$SH);
			$this->assign('KN',$KN);
			$this->assign('LC',$LC);
			$this->assign('VC',$VC);
			$this->assign('ZA',$ZA);
			$this->assign('KR',$KR);
			$this->assign('SR',$SR);
			$this->assign('SJ',$SJ);
			$this->assign('SZ',$SZ);
			$this->assign('TJ',$TJ);
			$this->assign('TW',$TW);
			$this->assign('TZ',$TZ);
			$this->assign('TH',$TH);
			$this->assign('TG',$TG);
			$this->assign('TO',$TO);
			$this->assign('TT',$TT);
			$this->assign('TD',$TD);
			$this->assign('CZ',$CZ);
			$this->assign('TN',$TN);
			$this->assign('TR',$TR);
			$this->assign('TM',$TM);
			$this->assign('TC',$TC);
			$this->assign('TV',$TV);
			$this->assign('UG',$UG);
			$this->assign('UA',$UA);
			$this->assign('HU',$HU);
			$this->assign('UY',$UY);
			$this->assign('US',$US);
			$this->assign('VU',$VU);
			$this->assign('VA',$VA);
			$this->assign('VE',$VE);
			$this->assign('AE',$AE);
			$this->assign('VN',$VN);
			$this->assign('WF',$WF);
			$this->assign('CY',$CY);
    	
    	
    	
    	
    	$this->assign('Land',$Land);
    	$this->assign('update_country',$update_country);
    	$this->assign('order_id',$order_id);
    	$this->assign('user_info',$user_info);
    	
    	
    	
    	$template=sendorder_edit_.$user_info['country'];
    	
    	
    	$this->display($template);
    	
    	
    	}
    	if($order_id_submit){
    		$user_info1=$models->where('sn='.$order_id_submit)->find();
    		$_SESSION['domain_edit']=$user_info1['domain'];
    		$map=array();
    		//$map['sn']=$order_id_submit;
    		$map['firstname']=$_POST['firstname'];
    		$map['lastname']=$_POST['lastname'];
    		$map['Firmenname']=$_POST['Firmenname'];
		$map['username']=$_POST['firstname'].' '.$_POST['lastname'];
		$map['consignee']=$map['username'];
    		$map['address1']=$_POST['address1'];
    		$map['address2']=$_POST['address2'];
    		$map['zipcode']=$_POST['zipcode'];
    		$map['zipcode2']=$_POST['zipcode2'];
    		$map['state']=$_POST['state'];
    		
    		$map['city']=$_POST['city'];
    		$map['country1']=$_POST['country'];
    		$map['Telefon']=$_POST['Telefon'];
    		$map['email']=$_POST['email'];
    		$map['night_phone_a']=$_POST['night_phone_a'];
			$map['night_phone_b']=$_POST['night_phone_b'];
			$map['night_phone_c']=$_POST['night_phone_c'];
    		$map['username']=$_POST['firstname'].' '.$_POST['lastname'];
    		$models = M("order");
    		$sessionsave=$models->where('sn='.$order_id_submit)->save($map);
    		$_SESSION['editsave']=1;
    		$user_info=$models->where('sn='.$order_id_submit)->find();
    		$ajaxretrun='<ul class="con_a2">
	   <li></li>
	   <li></li>
	   <li></li>
	   <li></li>
	   <li></li>
       <li></li>
	   </ul><ul class="con_a3">
       <li>'.$user_info['username'].'</li>
	   <li>'.$user_info['address1'].'</li>
	   <li>'.$user_info['address2'].'</li>
	   <li>'.$user_info['zipcode'].'</li>
	   <li>'.$user_info['city'].'</li>
       <li>'.$user_info['email'].'</li>
       </ul>';
    		echo $ajaxretrun;
    		 

    		
    	}
    	
    	//$this->display("edit");
    }

    public function sendorder()
    {
    	
    	if(!$_SESSION['createorderid']&&$_SESSION['cart1']){
			$_post['sn_id']='001';
		
			if($orderinfo=M("order")->order('id desc')->find()){
				
				if(date('Ymd',time())==date('Ymd',$orderinfo['createtime'])){
				
					$newno=$orderinfo['snid']+1;
					
					
					if(strlen($newno)==1){
						$_post['sn_id']='00'.$newno;
					}elseif(strlen($newno)==2){
						$_post['sn_id']='0'.$newno;
					}else{
						$_post['sn_id']=$newno;
					}
				}
			}

				$orderid=date('Ymd',time()).$_post['sn_id'];
				$map=array();
				$map['sn']=$orderid;
				$map['snid']=$_post['sn_id'];
				$map['ip']=get_client_ip();
				$map['createtime']=time();
			    $id=M("order")->data($map)->add();
					if(false===$id){
						$this->error('Send Fail!');
						exit;
					}
				$_SESSION['createordersn_id']=$_post['sn_id'];
				$_SESSION['createorderid']=$orderid;

		
		}
		
		if($_POST){
    	$map1['domain']=$_POST['domain'];
		$map1['currency']=$_POST['currency_code'];
    	$siteinfo1=M('website')->where($map1)->find();
    	$_SESSION['a']['saleser'] = $siteinfo1['saleser'];
		}
    	

    	
    	/**存储订单信息*/
		if($_POST['domain']){
   			 $temp_domain=$_POST['domain'];
		}else {
			$temp_domain=$_SESSION['domain_edit'];
			unset($_SESSION['domain_edit']);
		}
			$temp_prolist=$_SESSION['product'][$temp_domain];
	    	foreach ($temp_prolist as $key=>$value){
						$prolist[]=$key;
	    	}
		if ($prolist){
			if(count($prolist)<=1){
				$cc=$this->prosplit($prolist[0],$temp_domain);
				$_SESSION['a_price']=$cc['n_price'];
				$a=array($cc);
			}else{
				//$split_dir1 = split ('[,.-]', $prolist);
				$split_dir1 = $prolist;
				$a=array();
				$_SESSION['a_price']='';
				for($i=0;$i<count($split_dir1);$i++){
					$cc=$this->prosplit($split_dir1[$i],$temp_domain);
					$_SESSION['a_price']+=$cc['n_price'];
					$a[$i]=$cc;
					
				}
				
			}
		$_post['sn_id']=$_SESSION['createordersn_id'];
		$orderid=$_SESSION['createorderid'];
		
	
		
		if($_POST['tracking']){$_POST['tracking']='是';}else{$_POST['tracking']='否';}
		
		$_POST['saleser']=$_SESSION['a']['saleser'];
		//$_POST['domain']=$_SESSION['a']['domain'];
		
		
		}
		
		
		foreach($a as $val){
			$totalnum+=$val['num'];
			$tprice+=$val['n_price'];
			
		}
		
		
		
		$tprice=$tprice+($_POST['logisticsprice']*$totalnum);
		$totalnumhj=$_POST['logisticsprice']*$totalnum;
		if($_POST['logisticsprice']){
		$_SESSION['templogisticsprice']=$_POST['logisticsprice'];
		}
		$totalnumhj=$_SESSION['templogisticsprice']*$totalnum;
		
	
		$map=array();
		$split_dir1 = split ('[,.-]', $prolist);
		$ii=0;
		for($i=0;$i<count($split_dir1);$i++){$ii=$ii+$_SESSION['n_'.$split_dir1[$i]];}
		$map['totalnum']=$ii;
		$map['productlist']=serialize($a);
		//$map['sn']=$orderid;
		//$map['snid']=$_post['sn_id'];
		$map['price']=$tprice;
		$map['num']=$totalnum;
		$map['firstname']=$_POST['first_name'];
		$map['lastname']=$_POST['last_name'];
		$map['username']=$_POST['first_name'].' '.$_POST['last_name'];
		$map['consignee']=$map['username'];
		
		
		$map['country1']=$_POST['country'];
		
		if($_POST['client_countryship']){$map['country1']=$_POST['client_countryship'];}
		
		
		$map['state']=$_POST['state'];
		$map['city']=$_POST['city'];
		$map['address1']=$_POST['address1'];
		$map['address2']=$_POST['address2'];
		$map['address']=$_POST['address1'].''.$_POST['address2'];
		
		$map['zipcode']=$_POST['zip'];
		$map['zipcode2']=$_POST['zip2'];
		$map['night_phone_a']=$_POST['night_phone_a'];
		$map['night_phone_b']=$_POST['night_phone_b'];
		$map['night_phone_c']=$_POST['night_phone_c'];
		$map['email']=$_POST['email'];
		$map['note']=$_POST['client_remark'];
		$map['tracking']=$_POST['tracking'];
		$map['destination']=$_POST['destination'];
		$map['logistics']=$_POST['transportation'];
		$map['website']=$_POST['sitecode'];
		$map['country']=$_SESSION['a']['country'];
		$map['saleser']=$_POST['saleser'];
		$map['currency']=$_POST['currency_code'];
		$map['domain']=$_POST['domain'];
		//if(empty($map['country1'])){$map['country1']=$map['country'];}
		//$map['ip']=get_client_ip();
		//$map['createtime']=time();
		
		
		
		
		
		$Language_shows=$map['country1'];
		
		
	
		
		$models = M("order");
		//if(!$models->where('sn='.$orderid)->find()){
		if(!$_SESSION['editsave']){
		  $id=$models->where('sn='.$orderid)->save($map); 
    	}
		//$id=$models->data($map)->add();
		if(false===$id){
			$this->error('Send Fail!');
			exit;
		}
    //}
		
		
		
		
		if(isset($_POST['__hash__'])){unset($_POST['__hash__']);}
		if(isset($_POST['x'])){unset($_POST['x']);}
		if(isset($_POST['y'])){unset($_POST['y']);}
		if(isset($_POST['saleser'])){unset($_POST['saleser']);}
		if(isset($_POST['domain'])){unset($_POST['domain']);}
		if(isset($_POST['tracking'])){unset($_POST['tracking']);}
 
		if($paypal=M('payment')->find()){
			$paypalname=$paypal['yourinfo'];
		}else{die('has not paypal account');}
		$user_info=$models->where('sn='.$orderid)->find();
		
		//$_POST[]=$user_info;
		if ($user_info['country']=="JP"){
			$_POST['client_companyship'] =$user_info['client_companyship'];
		    $_POST['client_companyship2'] =$user_info['client_companyship2'];
		    $_POST['DIVISION'] =$user_info['DIVISION'];
		    $_POST['first_name'] =$user_info['firstname'];
		    $_POST['last_name'] =$user_info['lastname'];
		    $_POST['zip'] =$user_info['zipcode'];
		    $_POST['zip2'] =$user_info['zipcode2'];
		    $_POST['zip']=$_POST['zip']."-".$_POST['zip2'];
		    $_POST['state'] =$user_info['state'];
		    $_POST['city'] =$user_info['city'];
		    $_POST['address1'] =$user_info['address1'];
		    $_POST['address2'] =$user_info['address2'];
		    $_POST['night_phone_a'] =$user_info['night_phone_a'];
		    $_POST['night_phone_b'] =$user_info['night_phone_b'];
		    $_POST['night_phone_c'] =$user_info['night_phone_c'];
		    $_POST['email'] =$user_info['email'];
		    $_POST['client_remark'] =$user_info['note'];
		    $_POST['country'] =$user_info['country'];
		    $_POST['destination'] =$user_info['destination'];
		    $_POST['transportation'] =$user_info['logistics'];
		    $_POST['logisticsprice'] =$_SESSION['templogisticsprice'];
		    $_POST['currency_code'] =$user_info['currency'];
		    $_POST['client_countryship'] =$user_info['country1'];
		}else {
			$_POST['first_name']=$user_info['firstname'];
			$_POST['last_name']=$user_info['lastname'];
			$_POST['address1']=$user_info['address1'];
			$_POST['address2']=$user_info['address2'];
			$_POST['zip']=$user_info['zipcode'];
			$_POST['zip2']=$user_info['zipcode2'];
			$_POST['city']=$user_info['city'];
			$_POST['client_countryship'] = $user_info['country1'];
 			$_POST['state'] =$user_info['state'];
			$_POST['night_phone_a']=$user_info['night_phone_a'];
			$_POST['night_phone_b']=$user_info['night_phone_b'];
			$_POST['night_phone_c']=$user_info['night_phone_c'];
			$_POST['email']=$user_info['email'];
			$_POST['client_remark']=$user_info['note'];
			$_POST['country']=$user_info['country'];
			$_POST['destination'] =$user_info['destination'];
	   		$_POST['transportation'] =$user_info['logistics'];
	   		$_POST['currency_code']=$user_info['currency'];
	   		$_POST['logisticsprice'] =$_SESSION['templogisticsprice'];
		}
		
		
		
			$Language_shows=$user_info['country1'];
		
			$lc_country=$_POST['country'];
			$_POST['country']=$user_info['country1'];

		

			$_url_1='https://www.sandbox.paypal.com/cgi-bin/webscr';// 沙盒地址 
			$_url_2='https://www.paypal.com/cgi-bin/webscr';//  正式地址
			$reurl='http://'.$_SERVER['HTTP_HOST'].'/index.php?m=Index&a=thanks';  //返回的页面
			$_notify_url="http://".$_SERVER['HTTP_HOST']."/index.php?m=Index&a=ipn"; //验证页面
			
			$pay='
			<form action="'.$_url_2.'" method="post" id="form_paypal" name="form_paypal" onsubmit="return delsession()">';
			
			
			foreach($_POST as $_k=>$_v){
				$pay.='<input type="hidden" name="'.$_k.'" value="'.$_v.'">';
			}
			
			
			$pay.='
			
			<input type="hidden" name="cmd" value="_cart">
			<input type="hidden" name="upload" value="1">
			
			
			<input type="hidden" name="lc" value="'.$lc_country.'">
			<input type="hidden" name="business" value="'.$paypalname.'">';
			
			
			//卢布美元换算传值
			if($lc_country=="RU"){
				$i=0;
				foreach($a as $k=>$val){
				$i++;
				$pay.='
				<input type="hidden" name="item_name_'.$i.'" value="'.$val['productcode'].'">
				<input type="hidden" name="amount_'.$i.'" value="'.round($val['price']*0.035,2).'">
				<input type="hidden" name="quantity_'.$i.'" value="'.$val['num'].'">
				<input type="hidden" name="shipping_'.$i.'" value="'.round($_POST['logisticsprice']*0.035,2).'" />
				<input type="hidden" name="shipping2_'.$i.'" value="'.round($_POST['logisticsprice']*0.035,2).'" />';
				
				//round($_POST['logisticsprice']*0.035,2)
				}
			}else {
			
				$i=0;
				foreach($a as $k=>$val){
				$i++;
				$pay.='
				<input type="hidden" name="item_name_'.$i.'" value="'.$val['productcode'].'">
				<input type="hidden" name="amount_'.$i.'" value="'.$val['price'].'">
				<input type="hidden" name="quantity_'.$i.'" value="'.$val['num'].'">
				<input type="hidden" name="shipping_'.$i.'" value="'.$_POST['logisticsprice'].'" />
				<input type="hidden" name="shipping2_'.$i.'" value="'.$_POST['logisticsprice'].'" />';
				
				}
			
			}
			
			
			$pay.='
			<input type="hidden" name="currency_code" value="'.$_POST['currency_code'].'">
			<input type="hidden" name="return" value="'.$reurl.'">
			<input type="hidden" name="invoice" value="'.$orderid.'">
			<input type="hidden" name="charset" value="utf-8">
			<input type="hidden" name="no_shipping" value="2">
			<input type="hidden" name="no_note" value="0">
			<input type="hidden" name="notify_url" value="'.$_notify_url.'">
			<input type="hidden" name="rm" value="2">
			<input type="hidden" name="cancel_return" value="'.$reurl.'">
			';
		
		
		
		
		
		
			
		/**
		 * 家国传值
		 */
		switch ($Language_shows){
				
				
				
				case "US":$country="United States";break;
				case "AL":$country="Albania";break;
				case "DZ":$country="Algeria";break;
				case "AD":$country="Andorra";break;
				case "AO":$country="Angola";break;
				case "AI":$country="Anguilla";break;
				case "AG":$country="Antigua and Barbuda";break;
				case "AR":$country="Argentina";break;
				case "AM":$country="Armenia";break;
				case "AW":$country="Aruba";break;
				case "AU":$country="Australia";break;
				case "AT":$country="Austria";break;
				case "AZ":$country="Azerbaijan Republic";break;
				case "BS":$country="Bahamas";break;
				case "BH":$country="Bahrain";break;
				case "BB":$country="Barbados";break;
				case "BE":$country="Belgium";break;
				case "BZ":$country="Belize";break;
				case "BJ":$country="Benin";break;
				case "BM":$country="Bermuda";break;
				case "BT":$country="Bhutan";break;
				case "BO":$country="Bolivia";break;
				case "BA":$country="Bosnia and Herzegovina";break;
				case "BW":$country="Botswana";break;
				case "BR":$country="Brazil";break;
				case "BN":$country="Brunei";break;
				case "BG":$country="Bulgaria";break;
				case "BF":$country="Burkina Faso";break;
				case "BI":$country="Burundi";break;
				case "KH":$country="Cambodia";break;
				case "CA":$country="Canada";break;
				case "CV":$country="Cape Verde";break;
				case "KY":$country="Cayman Islands";break;
				case "TD":$country="Chad";break;
				case "CL":$country="Chile";break;
				case "C2":$country="China";break;
				case "CO":$country="Colombia";break;
				case "KM":$country="Comoros";break;
				case "CK":$country="Cook Islands";break;
				case "CR":$country="Costa Rica";break;
				case "HR":$country="Croatia";break;
				case "CY":$country="Cyprus";break;
				case "CZ":$country="Czech Republic";break;
				case "CD":$country="Democratic Republic of the Congo";break;
				case "DK":$country="Denmark";break;
				case "DJ":$country="Djibouti";break;
				case "DM":$country="Dominica";break;
				case "DO":$country="Dominican Republic";break;
				case "EC":$country="Ecuador";break;
				case "SV":$country="El Salvador";break;
				case "ER":$country="Eritrea";break;
				case "EE":$country="Estonia";break;
				case "ET":$country="Ethiopia";break;
				case "FK":$country="Falkland Islands";break;
				case "FO":$country="Faroe Islands";break;
				case "FJ":$country="Fiji";break;
				case "FI":$country="Finland";break;
				case "FR":$country="France";break;
				case "GF":$country="French Guiana";break;
				case "PF":$country="French Polynesia";break;
				case "GA":$country="Gabon Republic";break;
				case "GM":$country="Gambia";break;
				case "DE":$country="Germany";break;
				case "GI":$country="Gibraltar";break;
				case "GR":$country="Greece";break;
				case "GL":$country="Greenland";break;
				case "GD":$country="Grenada";break;
				case "GP":$country="Guadeloupe";break;
				case "GT":$country="Guatemala";break;
				case "GN":$country="Guinea";break;
				case "GW":$country="Guinea Bissau";break;
				case "GY":$country="Guyana";break;
				case "HN":$country="Honduras";break;
				case "HK":$country="Hong Kong";break;
				case "HU":$country="Hungary";break;
				case "IS":$country="Iceland";break;
				case "IN":$country="India";break;
				case "ID":$country="Indonesia";break;
				case "IE":$country="Ireland";break;
				case "IL":$country="Israel";break;
				case "IT":$country="Italy";break;
				case "JM":$country="Jamaica";break;
				case "JP":$country="Japan";break;
				case "JO":$country="Jordan";break;
				case "KZ":$country="Kazakhstan";break;
				case "KE":$country="Kenya";break;
				case "KI":$country="Kiribati";break;
				case "KW":$country="Kuwait";break;
				case "KG":$country="Kyrgyzstan";break;
				case "LA":$country="Laos";break;
				case "LV":$country="Latvia";break;
				case "LS":$country="Lesotho";break;
				case "LI":$country="Liechtenstein";break;
				case "LT":$country="Lithuania";break;
				case "LU":$country="Luxembourg";break;
				case "MG":$country="Madagascar";break;
				case "MW":$country="Malawi";break;
				case "MY":$country="Malaysia";break;
				case "MV":$country="Maldives";break;
				case "ML":$country="Mali";break;
				case "MT":$country="Malta";break;
				case "MH":$country="Marshall Islands";break;
				case "MQ":$country="Martinique";break;
				case "MR":$country="Mauritania";break;
				case "MU":$country="Mauritius";break;
				case "YT":$country="Mayotte";break;
				case "MX":$country="Mexico";break;
				case "FM":$country="Micronesia";break;
				case "MN":$country="Mongolia";break;
				case "MS":$country="Montserrat";break;
				case "MA":$country="Morocco";break;
				case "MZ":$country="Mozambique";break;
				case "NA":$country="Namibia";break;
				case "NR":$country="Nauru";break;
				case "NP":$country="Nepal";break;
				case "NL":$country="Netherlands";break;
				case "AN":$country="Netherlands Antilles";break;
				case "NC":$country="New Caledonia";break;
				case "NZ":$country="New Zealand";break;
				case "NI":$country="Nicaragua";break;
				case "NE":$country="Niger";break;
				case "NU":$country="Niue";break;
				case "NF":$country="Norfolk Island";break;
				case "NO":$country="Norway";break;
				case "OM":$country="Oman";break;
				case "PW":$country="Palau";break;
				case "PA":$country="Panama";break;
				case "PG":$country="Papua New Guinea";break;
				case "PE":$country="Peru";break;
				case "PH":$country="Philippines";break;
				case "PN":$country="Pitcairn Islands";break;
				case "PL":$country="Poland";break;
				case "PT":$country="Portugal";break;
				case "QA":$country="Qatar";break;
				case "CG":$country="Republic of the Congo";break;
				case "RE":$country="Reunion";break;
				case "RO":$country="Romania";break;
				case "RU":$country="Russia";break;
				case "RW":$country="Rwanda";break;
				case "KN":$country="Saint Kitts and Nevis Anguilla";break;
				case "PM":$country="Saint Pierre and Miquelon";break;
				case "VC":$country="Saint Vincent and Grenadines";break;
				case "WS":$country="Samoa";break;
				case "SM":$country="San Marino";break;
				case "ST":$country="São Tomé and Príncipe";break;
				case "SA":$country="Saudi Arabia";break;
				case "SN":$country="Senegal";break;
				case "SC":$country="Seychelles";break;
				case "SL":$country="Sierra Leone";break;
				case "SG":$country="Singapore";break;
				case "SK":$country="Slovakia";break;
				case "SI":$country="Slovenia";break;
				case "SB":$country="Solomon Islands";break;
				case "SO":$country="Somalia";break;
				case "ZA":$country="South Africa";break;
				case "KR":$country="South Korea";break;
				case "ES":$country="Spain";break;
				case "LK":$country="Sri Lanka";break;
				case "SH":$country="St. Helena";break;
				case "LC":$country="St. Lucia";break;
				case "SR":$country="Suriname";break;
				case "SJ":$country="Svalbard and Jan Mayen Islands";break;
				case "SZ":$country="Swaziland";break;
				case "SE":$country="Sweden";break;
				case "CH":$country="Switzerland";break;
				case "TW":$country="Taiwan";break;
				case "TJ":$country="Tajikistan";break;
				case "TZ":$country="Tanzania";break;
				case "TH":$country="Thailand";break;
				case "TG":$country="Togo";break;
				case "TO":$country="Tonga";break;
				case "TT":$country="Trinidad and Tobago";break;
				case "TN":$country="Tunisia";break;
				case "TR":$country="Turkey";break;
				case "TM":$country="Turkmenistan";break;
				case "TC":$country="Turks and Caicos Islands";break;
				case "TV":$country="Tuvalu";break;
				case "UG":$country="Uganda";break;
				case "UA":$country="Ukraine";break;
				case "AE":$country="United Arab Emirates";break;
				case "GB":$country="United Kingdom";break;
				case "UY":$country="Uruguay";break;
				case "VU":$country="Vanuatu";break;
				case "VA":$country="Vatican City State";break;
				case "VE":$country="Venezuela";break;
				case "VN":$country="Vietnam";break;
				case "VG":$country="Virgin Islands (British)";break;
				case "WF":$country="Wallis and Futuna Islands";break;
				case "YE":$country="Yemen";break;
				case "ZM":$country="Zambia";break;
				default:$country="";
		}
		
		//传值
	
		$this->assign ( 'currency', $_POST['currency_code'] );
		$this->assign ( 'a', $a );
		$this->assign ( 'post', $_POST );
		$this->assign ( 'orderid', $orderid );
		$this->assign ( 'tprice', $user_info['price'] );
		$this->assign ('logisticsprice', $totalnumhj);
		
		$this->assign('logo',$user_info['domain']);
		$this->assign ( 'orderid', $orderid );
		$this->assign('country1',$user_info['country1']);
		$this->assign('country',$country);
		$this->assign('username',$user_info['username']);
		$this->assign('address1',$user_info['address1']);
		$this->assign('address2',$user_info['address2']);
		$this->assign('zipcode',$user_info['zipcode']);
		$this->assign('zipcode2',$user_info['zipcode2']);
		
		$this->assign('state',$user_info['state']);
		$this->assign('city',$user_info['city']);
		$this->assign('email',$user_info['email']);
		$this->assign ('pay', $pay);
		
	
    	//$this->display("sendorder");
    	//print_r($_POST);
    	
  	    if(!$siteinfo1){
	    	$map1['domain']=$user_info['domain'];
			//$map1['currency']=$user_info['currency_code'];
	    	$siteinfo1=M('website')->where($map1)->find();
		}
    	$infotemplate = split ('[,.-]', $siteinfo1['infotemplate']);
    	$this->display($infotemplate[0]);
    	
	}
    
    public function delsessions(){
    	unset($_SESSION['cart1']);
		unset($_SESSION['a_price']);
		unset($_SESSION['a']);
		unset($_SESSION['product']);
		unset($_SESSION['createordersn_id']);
		unset($_SESSION['createorderid']);
		unset($_SESSION['editsave']);
    }
    
    
	public function thanks() {
			if($_SESSION['thanks']['jumpUrl']){$jumpUrl=$_SESSION['thanks']['jumpUrl'];}else{$jumpUrl='http://'.$_SERVER['HTTP_HOST'];}
			if($_SESSION['thanks']['success']){$_success=$_SESSION['thanks']['success'];}else{$jumpUrl='购买成功,我们已经收到了您的订单信息,如有问题请联系我们!';}
			if($_SESSION['thanks']['error']){$_error=$_SESSION['thanks']['error'];}else{$jumpUrl='订单支付失败！';}
			$this->assign ( 'jumpUrl', $jumpUrl);
			if($this->respond()){$this -> success($_success);}else{$this->error($_error);}
			unset($_SESSION['thanks']);
	}
	public function respond()
    {
		$order_sn = $_POST['invoice'];	
		$models = M("order");
		if(!$rs=$models->where('sn="'.$order_sn.'"')->find())
		{return false;}
		if($rs['status']==4){return true;}else{return false;}
    }

	public function ipn()
    {
		if($payment=M('payment')->find()){
			$merchant_id = $payment['yourinfo'];               ///获取商户收款账号
		}else{return false;}

/*
	@$fsp = fopen("test_post.txt","w"); 
	if(!$fsp){ 

	}else { 
	fwrite($fsp,$a); 
	fclose($fsp); 
	}
*/


        $req = 'cmd=_notify-validate';
        foreach ($_POST as $key => $value){
            $value = urlencode(stripslashes($value));
            $req .= "&$key=$value";
        }

        $header = "POST /cgi-bin/webscr HTTP/1.0\r\n";
        $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
        $header .= "Content-Length: " . strlen($req) ."\r\n\r\n";
        $fp = fsockopen ('www.paypal.com', 80, $errno, $errstr, 30);


        $item_name = $_POST['item_name'];			//商品名称(这里是订单号)
        $item_number = $_POST['item_number'];		//数量
        $payment_status = $_POST['payment_status'];	//支付状态
        $payment_amount = $_POST['mc_gross'];		//金额
        $payment_currency = $_POST['mc_currency'];	//币种
        $txn_id = $_POST['txn_id'];					//(paypal 交易号)
        $receiver_email = $_POST['receiver_email'];	//收款人（即商家）的主要邮件地址
        $business = $_POST['business'];	//收款人（即商家）的电子邮件地址或账户号。如果付款发送至主账户，则等于receiver_email，
        $payer_email = $_POST['payer_email'];		//付款账号
        $order_sn = $_POST['invoice'];				//订单号

        $address_name = $_POST['address_name'];
        $address_zip = $_POST['address_zip'];
        $address_country = $_POST['address_country'];
        $address_state = $_POST['address_state'];
        $address_city = $_POST['address_city'];
        $address_street = $_POST['address_street'];
        $first_name = $_POST['first_name'];
        $last_name = $_POST['last_name'];

        $memo = !empty($_POST['memo']) ? $_POST['memo'] : '';
        $action_note = $txn_id . '（paypal txn_id）' . $memo;

        if (!$fp){
            fclose($fp);
            return false;
        }else{
            fputs($fp, $header . $req);
            while (!feof($fp))
            {
                $res = fgets($fp, 1024);
                if (stripos($res, 'VERIFIED') >= 0){
                    if ($payment_status != 'Completed'){
                        fclose($fp);
                        return false;
                    }

                    if ($receiver_email != $merchant_id && $business != $merchant_id){
                        fclose($fp);
                        return false;
                    }

					$models = M("order");
					if(!$rs=$models->where('sn="'.$order_sn.'"')->find()){
                        fclose($fp);
                        return false;
                    }
                    if ($rs['price'] != $payment_amount){
                        fclose($fp);
                        return false;
                    }
                    if ($rs['currency'] != $payment_currency){
                        fclose($fp);
                        return false;
                    }

					$models->where('sn="'.$order_sn.'"')->setField('status',4);
					$models->where('sn="'.$order_sn.'"')->setField('ispayment',1);
					$models->where('sn="'.$order_sn.'"')->setField('email',$payer_email);

					if($txn_id){$models->where('sn="'.$order_sn.'"')->setField('txn_id',$txn_id);}
					if($memo){$models->where('sn="'.$order_sn.'"')->setField('note',$memo);}

					if($first_name){$models->where('sn="'.$order_sn.'"')->setField('firstname',$first_name);}
					if($last_name){$models->where('sn="'.$order_sn.'"')->setField('lastname',$last_name);}
					if($address_name){$models->where('sn="'.$order_sn.'"')->setField('consignee',$address_name);}
					if($address_zip){$models->where('sn="'.$order_sn.'"')->setField('zipcode',$address_zip);}
					if($address_country){$models->where('sn="'.$order_sn.'"')->setField('country1',$address_country);}
					if($address_state){$models->where('sn="'.$order_sn.'"')->setField('state',$address_state);}
					if($address_city){$models->where('sn="'.$order_sn.'"')->setField('city',$address_city);}
					if($address_street){$models->where('sn="'.$order_sn.'"')->setField('address1',$address_street);$models->where('sn="'.$order_sn.'"')->setField('address2','');}





					$a='';
					foreach($_REQUEST as $_k=>$_v){$a.=$_k.':'.$_v.' <br /> ';}
					$models->where('sn="'.$order_sn.'"')->setField('ipn',$a);
                    fclose($fp);
                    return true;
                }
            }
            fclose($fp);
            return false;
        }
    }
}
?>